#include "GameSceneManager.h"
#include "GameObjectManager.h"
#include "Ape/InputHandler.h"
#include "Ape/GUISystem.h"
#include "Ape/WindowEventHandler.h"
#include "CMesh.h"
#include "CRender.h"
#include "CLight.h"
#include "CCamera.h"
#include "CPosition.h"
#include "COrientation.h"
#include "CVelocity.h"
#include "CInputPlayer.h"
#include "Physics.h"
#include "CPhysics.h"
#include "TextOutput.h"

#include "Loader.h"

GameSceneManager* GameSceneManager::mInstance = NULL;
Root* GameSceneManager::mRoot = NULL;
Camera* GameSceneManager::mCamera = NULL;
SceneNode* GameSceneManager::mCameraNode = NULL;
Viewport* GameSceneManager::mViewport = NULL;
SceneManager* GameSceneManager::mSceneMgr = NULL;
RenderWindow* GameSceneManager::mWindow = NULL;

SceneNode* GameSceneManager::mCameraPitchNode = NULL;
SceneNode* GameSceneManager::mCameraYawNode = NULL;

XMLHandler* GameSceneManager::xmlh = NULL;
//TiXmlElement* GameSceneManager::pElem = NULL;

//TiXmlHandle* GameSceneManager::hDoc = NULL;
//TiXmlHandle* GameSceneManager::hRoot = NULL;
//const char* GameSceneManager::currentMap = "";

GameSceneManager::GameSceneManager() {

}

GameSceneManager::~GameSceneManager() {
	if(mInstance != NULL)
		delete mInstance;
}

GameSceneManager* GameSceneManager::getSingleton() {
	if(mInstance == NULL)
		mInstance = new GameSceneManager();
	return mInstance;
}

Root* GameSceneManager::getRoot() {
	return mRoot;
}

SceneManager* GameSceneManager::getSceneManager() {
	return mSceneMgr;
}

Viewport* GameSceneManager::getViewport() {
	return mViewport;
}
Camera* GameSceneManager::getCamera() {
	return mCamera;
}
void GameSceneManager::saveMap(std::string mapFileName) {
	if(mapFileName == "")
		return;

	GameSystemManager::getSingleton()->getGUI()->showLoader(true);

	TiXmlElement* mTerrElem;
	TiXmlElement* mSkyElem;
	TiXmlElement* mObjects;
	TiXmlElement* mPagedObjects;

	TiXmlHandle hRoot(0);
	TiXmlAttribute* pAttrib; 
	TiXmlDocument doc(mapFileName.c_str());
	TiXmlHandle hDoc(&doc);
	TiXmlElement* mRootObject;

	GameSystemManager::getSingleton()->getTerrain()->saveTerrain(mapFileName);

	TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );  
	doc.LinkEndChild(decl);

	mRootObject = new TiXmlElement( "ApeEngine" );
	doc.LinkEndChild( mRootObject );

	TiXmlComment * comment = new TiXmlComment();
	comment->SetValue("Ape Engine map file :)" );  
	mRootObject->LinkEndChild( comment );  

	if (!mRootObject) 
		return;

	mTerrElem = new TiXmlElement("Terrain");  
	mRootObject->LinkEndChild(mTerrElem);

	mSkyElem = new TiXmlElement("Sky");  
	mRootObject->LinkEndChild(mSkyElem);
	
	mObjects = new TiXmlElement("Objects");  
	mRootObject->LinkEndChild(mObjects);
	
	mPagedObjects = new TiXmlElement("PagedGeometry");  
	mRootObject->LinkEndChild(mPagedObjects);

	//create if not already
	if(mTerrElem)
		GameSystemManager::getSingleton()->getTerrain()->saveAttrib(mTerrElem);

	if(mSkyElem)
		GameSystemManager::getSingleton()->getSky()->saveAttrib(mSkyElem);
	
	if(mObjects)
		//GameSystemManager::getSingleton()->getSky()->saveAttrib(mObjects);
		GameObjectManager::getSingleton()->saveGameObjectsToXML(mObjects);

	if(mPagedObjects)
		GameSystemManager::getSingleton()->getPaging()->saveGameObjectsToXML(mPagedObjects);

	mapFileName += ".xml";

	std::string mPath = "../../Media/Maps/" + mapFileName;

	doc.SaveFile(mPath.c_str()); // + mFileName.c_str()
	
	GameSystemManager::getSingleton()->getGUI()->showLoader(false);

	//GameSystemManager::getSingleton()->getTerrain()->saveSettings(mapFileName.c_str());

}
/*
void getdir (string dir, vector<string> &files)
{
	WIN32_FIND_DATA findFileData;
	HANDLE hFind = FindFirstFile((LPCWSTR)"*", &findFileData);

	if(hFind  == INVALID_HANDLE_VALUE) {
		std::cout <<"No files found." <<std::endl;
	} else {
		std::cout <<"Files found." <<std::endl;
	}

}
*/
/*
void lsdir(const char *path, vector<string> &results)	
{
	WIN32_FIND_DATA fdata;
	HANDLE dhandle;

	// must append \* to the path
	{
		char buf[MAX_PATH];
		sprintf(buf, sizeof(buf), "%s\\*", path);
		if((dhandle = FindFirstFile(buf, &fdata)) == INVALID_HANDLE_VALUE) {
			throw Error("FindFirstFile");
		}
	}

	// even an "empty" directory will give two results - . and ..
	results.append(String(fdata.cFileName));

	while(1) {
		if(FindNextFile(dhandle, &fdata)) {
			results.append(String(fdata.cFileName));
		} else {
			if(GetLastError() == ERROR_NO_MORE_FILES) {
				break;
			} else {
				FindClose(dhandle);
				throw Error("FindNextFile");
			}
		}
	}

	if(FindClose(dhandle) == 0) {
		throw Error("FindClose");
	}
}
*/
void GameSceneManager::loadMap(std::string pFilename)
{
	pFilename += ".xml";

	std::string mPath = "../../Media/Maps/" + pFilename;

	TiXmlDocument doc(mPath.c_str());
	
	if (!doc.LoadFile()) 
		return;

	GameSystemManager::getSingleton()->getGUI()->showLoader(true);

	TiXmlHandle hDoc(&doc);
	TiXmlElement* pElem;
	TiXmlHandle hRoot(0);

	// block: name
	pElem=hDoc.FirstChildElement().Element();
	// should always have a valid root but handle gracefully if it does
	if (!pElem) return;
	const char* m_name=pElem->Value();

	// save this for later
	hRoot=TiXmlHandle(pElem);

	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Terrain

	pElem=hRoot.FirstChild("Terrain").Element();
	TiXmlAttribute* pAttrib; 
	
	pAttrib = pElem->FirstAttribute();
	while (pAttrib)
	{
		std::string attribName = pAttrib->Name();
		std::string attribValue = pAttrib->Value();

		GameSystemManager::getSingleton()->getTerrain()->setAttribute(attribName,attribValue);
		pAttrib=pAttrib->Next();			
	}

	if(terrain_config.mIsTerrainLoaded)
		GameSystemManager::getSingleton()->getTerrain()->removeTerrain();

	
	GameSystemManager::getSingleton()->getTerrain()->generateTerrain();
	//GameSystemManager::getSingleton()->getGUI()->showLoader(false);


	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Sky

	GameSystemManager::getSingleton()->getSky()->destroySky();

	pElem=hRoot.FirstChild("Sky").Element();
	//TiXmlAttribute* pAttrib; 

	pAttrib = pElem->FirstAttribute();
	while (pAttrib)
	{
		std::string attribName = pAttrib->Name();
		std::string attribValue = pAttrib->Value();

		GameSystemManager::getSingleton()->getSky()->setAttribute(attribName,attribValue);
		pAttrib=pAttrib->Next();			
	}
	GameSystemManager::getSingleton()->getGUI()->fillGUIFromConfig();
	/////////////////////////////////////////////////////////////////////////////////////////////////////////

	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Paged Geometry

	GameSystemManager::getSingleton()->getPaging()->removeAllPagedObjects();
	GameSystemManager::getSingleton()->getGUI()->updatePagedObjects();
	pElem=hRoot.FirstChild("PagedGeometry").Element();
	if(pElem)
		GameSystemManager::getSingleton()->getPaging()->loadGameObjectsToXML(pElem);

	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Scene Objects
	pElem=hRoot.FirstChild("Objects").Element();
	if(pElem)
		GameObjectManager::getSingleton()->loadGameObjectsFromXML(pElem);
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////

	//GameSystemManager::getSingleton()->getGUI()->showLoader(true);
	GameSystemManager::getSingleton()->getSky()->createSky();

	//GameSystemManager::getSingleton()->getPhysics()->shutdown();
	//GameSystemManager::getSingleton()->createPhysics();

	GameSystemManager::getSingleton()->getPhysics()->createScene();

	//if()
	//loadSceneFile("test.scene");

	GameSystemManager::getSingleton()->getGUI()->showLoader(false);
	

}

void GameSceneManager::loadSceneFile(std::string sceneFile) {
	
	OgreMaxSceneCallbacks* mSceneCallbacks = new OgreMaxSceneCallbacks(GameSystemManager::getSingleton()->getPhysics()->getWorld(),Vector3(800,200,680) );
	
	OgreMaxScene* OMscene; // = NULL;
	OMscene = new OgreMax::OgreMaxScene ();
	OMscene->Load(sceneFile, GameSystemManager::getSingleton()->getOgre()->getRoot()->getAutoCreatedWindow(),
					OgreMax::OgreMaxScene::NO_OPTIONS, NULL, NULL, mSceneCallbacks);
	
}

void GameSceneManager::create() {  //GameSystemManager* mGSM
	mRoot = GameSystemManager::getSingleton()->getOgre()->getRoot();
	mWindow = GameSystemManager::getSingleton()->getOgre()->getWindow();
	setupSceneManager();
	createCamera();
	setupLights();

	xmlh = new XMLHandler();
	xmlh->initFile();
}

void GameSceneManager::setupSceneManager() {
	/*
	ST_GENERIC = 1,
	ST_EXTERIOR_CLOSE = 2,
	ST_EXTERIOR_FAR = 4,
	ST_EXTERIOR_REAL_FAR = 8,
	ST_INTERIOR = 16
	*/
	mSceneMgr = mRoot->createSceneManager("OctreeSceneManager", "ETInstance");
	
	//mSceneMgr->setSkyBox(true,"SkyBox_morning");
	//mSceneMgr->setSkyBox(true, "3D-Diggers/SkyBox", 1000);
	
	

	//mSceneMgr->setSkyBox(true,"Examples/SpaceSkyPlane"); 
}

void GameSceneManager::setupLights() {
	//Set up lighting
	//Light *light = mSceneMgr->createLight("Sun"); //mSceneMgr->
	//light->setpo
	//light->setType(Light::LT_DIRECTIONAL); // LT_DIRECTIONAL
	//light->setDirection(Vector3(0.0f, -0.5f, 1.0f));
	mSceneMgr->setAmbientLight(ColourValue(0.6, 0.6, 0.6));
/*
	  Light *light = mSceneMgr->createLight("Sun");
	  light->setType(Light::LT_POINT);
	  light->setPosition(600,300,280);
*/
}
void GameSceneManager::createCamera()
{
	Vector3 camp; // = mCamera->getPosition();
	camp.x = 0; //600
	camp.z = 1; //300
	camp.y = 10; //280 HeightFunction::getTerrainHeight(camp.x, camp.z) + 3;; //HeightFunction::getTerrainHeight(camp.x, camp.z) + 3;
	
	//Initialize the camera and viewport
	//GameSystemManager::
	
	std::map<std::string,int> v;
	v.insert(std::pair<std::string, int>("1", 1));
	std::map<std::string,int>::iterator iter = v.begin();
	iter = v.find("2");
	int r = (iter != v.end()) ? iter->second : 0;

	mCamera =  mSceneMgr->createCamera("MainCamera");

	//mCamera->setPosition(camp);
	mViewport = mWindow->addViewport(mCamera);
	mViewport->setBackgroundColour(ColourValue(0.47f, 0.67f, 0.96f));	//Blue sky background color
	mCamera->setAspectRatio(Real(mViewport->getActualWidth()) / Real(mViewport->getActualHeight()));
	mCamera->setNearClipDistance(1.0f);
	mCamera->setFarClipDistance(1000.0f);
	
	//HeightFunction::initialize(mSceneMgr);	
	//camNode->setPosition( 40, 300, 580 );

	//mCamera->setPolygonMode(PM_SOLID);
	//mCamera->setPolygonMode(PM_WIREFRAME);

	////////////////////////////////////////////////////////////////////////////////////////
	//mCamera->setPosition(Vector3(0,1,10));
	//mCamera->lookAt(Vector3(0,2,-300));
	/*
	Plane plane2( Vector3::UNIT_Y, 0 );
	MeshManager::getSingleton().createPlane("ground",
		ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane2,
		400,400,10,10,true,1,25,25,Vector3::UNIT_Z);

	*/
	
	//Ogre::Entity* groundPlane = mSceneMgr->createEntity( "ninja", "ninja.mesh" );
	//groundPlane->setMaterialName("concrete");
	//groundPlane->setCastShadows(false);
	
	//groundPlane->setCastShadows(false);

	//groundNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	//groundNode->attachObject(groundPlane);
	//groundNode->setPosition(Vector3(730,50,720));
	
	//mSceneMgr->setShadowTechnique(SHADOWTYPE_TEXTURE_MODULATIVE);
	//SHADOWTYPE_STENCIL_ADDITIVE
	//SHADOWTYPE_TEXTURE_ADDITIVE
	//SHADOWTYPE_TEXTURE_MODULATIVE

	//mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(groundNode);
	////////////////////////////////////////////////////////////////////////////////////////

	//mSceneMgr->setWorldGeometry("Terrain.cfg");
	//createTerrain("Terrain.cfg");
}


void GameSceneManager::start() {

	InputHandler *mInputHandler = GameSystemManager::getSingleton()->getInput();
	GUISystem *mGUI = GameSystemManager::getSingleton()->getGUI();
	WindowEventHandler *mWindowHandler = GameSystemManager::getSingleton()->getWindowHandler();
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////

	GameObject *GO = new GameObject("Camera"); //(std::string)
	CCamera *myCamera = new CCamera();
	myCamera->setup();
	GO->setComponent(myCamera);
	CPosition *mPos = new CPosition();
	GO->setComponent(mPos);
	COrientation *mOr = new COrientation();
	GO->setComponent(mOr);
	CVelocity *mVel = new CVelocity();
	GO->setComponent(mVel);
	CInputPlayer *mInput = new CInputPlayer();
	mInput->setup();
	GO->setComponent(mInput);
	
	//Save camera node to the scene node
	mCameraNode = mInput->cameraNode;
	mCameraPitchNode = mInput->cameraPitchNode;
	mCameraYawNode = mInput->cameraYawNode;

	GameObjectManager::getSingleton()->createCustomObject(GO);

	//////////////////////////////////////////////////////////////////////////////////////////////////////
	//Create object from template
	//CCameraTemplate *mCamTempl = new CCameraTemplate();
	//GameObjectManager::getSingleton()->registerComponentTemplate( mCamTempl );
	//GameObjectTemplate *objTemplate = new GameObjectTemplate("CameraTemplate");
	//objTemplate->addCompTemplate(mCamTempl);
	//GameObjectManager::getSingleton()->registerObjectTemplate(objTemplate);
	//GameObjectManager::getSingleton()->createObjectFromTemplate("CameraTemplate","myCamera");
	//////////////////////////////////////////////////////////////////////////////////////////////////////

	CPositionTemplate *mCPosTempl = new CPositionTemplate();
	GameObjectManager::getSingleton()->registerComponentTemplate( mCPosTempl );
	COrientationTemplate *mCOrTempl = new COrientationTemplate();
	GameObjectManager::getSingleton()->registerComponentTemplate( mCOrTempl );
	CMeshTemplate *mCMeshTempl = new CMeshTemplate();
	GameObjectManager::getSingleton()->registerComponentTemplate( mCMeshTempl );
	CRenderTemplate *mCRenderTempl = new CRenderTemplate();
	GameObjectManager::getSingleton()->registerComponentTemplate( mCRenderTempl );
	CLightTemplate *mCLightTempl = new CLightTemplate();
	GameObjectManager::getSingleton()->registerComponentTemplate( mCLightTempl );
	CPhysicsTemplate *mCPhysicsTempl = new CPhysicsTemplate();
	GameObjectManager::getSingleton()->registerComponentTemplate( mCPhysicsTempl );

	//GameObjectTemplate *objTemplate = new GameObjectTemplate("MeshObjectTemplate");
	//objTemplate->addCompTemplate(mCPosTempl);
	//objTemplate->addCompTemplate(mCOrTempl);
	//objTemplate->addCompTemplate(mCMeshTempl);
	//objTemplate->addCompTemplate(mCRenderTempl);
	//objTemplate->addCompTemplate(mCLightTempl);
	//GameObjectManager::getSingleton()->registerObjectTemplate(objTemplate);
	
	//GameObjectManager::getSingleton()->createObjectFromTemplate("CameraTemplate","myCamera");


	//GameObject *GO2 = new GameObject("Camera2"); //(std::string)
	//GO2->setComponent(myCamera2);
	//GameObjectManager::getSingleton()->createCustomObject(GO2);
	//GameObjectManager::getSingleton()->registerObjectTemplate(GO2);

	//GameObject *GO2 = new GameObject("Camera2"); //(std::string)
	//CCamera *myCamera2 = new CCamera();
	//GO2->setComponent(myCamera2);

	//ObjectTemplate *ot;
	//ComponentTemplate *ct;
	//ct->
	
	//GameObjectManager::getSingleton()->registerObjectTemplate(GO2);

	//ApeComponent::CompTemplateMgr::getInstance()->registerComponent(myCamera);

	//GO->setComponent(ApeComponent::CompTemplateMgr::getInstance()->createComp((std::string)"CCamera"));

	//Ogre::Vector3(690,380,300)
	//Ogre::Entity* ent = GameSceneManager::getSingleton()->getSceneManager()->createEntity("kaka","horse.mesh");
	//Ogre::SceneNode *sNode = GameSceneManager::getSingleton()->getSceneManager()->getRootSceneNode()->createChildSceneNode();
	//sNode->attachObject(ent);
	//sNode->setPosition(690,380,300);
	

/*
		CompositorPtr comp = CompositorManager::getSingleton().create(
				"Bloom", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME
			);
		{
			CompositionTechnique *t = comp->createTechnique();
			{
				CompositionTechnique::TextureDefinition *def = t->createTextureDefinition("rt0");
				def->width = 128;
				def->height = 128;
				def->format = PF_A8R8G8B8;
			}
			{
				CompositionTechnique::TextureDefinition *def = t->createTextureDefinition("rt1");
				def->width = 128;
				def->height = 128;
				def->format = PF_A8R8G8B8;
			}
			{
				CompositionTargetPass *tp = t->createTargetPass();
				tp->setInputMode(CompositionTargetPass::IM_PREVIOUS);
				tp->setOutputName("rt1");
			}
			{
				CompositionTargetPass *tp = t->createTargetPass();
				tp->setInputMode(CompositionTargetPass::IM_NONE);
				tp->setOutputName("rt0");
				CompositionPass *pass = tp->createPass();
				pass->setType(CompositionPass::PT_RENDERQUAD);
				pass->setMaterialName("Ogre/Compositor/Blur0");
				pass->setInput(0, "rt1");
			}
			{
				CompositionTargetPass *tp = t->createTargetPass();
				tp->setInputMode(CompositionTargetPass::IM_NONE);
				tp->setOutputName("rt1");
				CompositionPass *pass = tp->createPass();
				pass->setType(CompositionPass::PT_RENDERQUAD);
				pass->setMaterialName("Ogre/Compositor/Blur1");
				pass->setInput(0, "rt0");
			}
			{
				CompositionTargetPass *tp = t->getOutputTargetPass();
				tp->setInputMode(CompositionTargetPass::IM_PREVIOUS);
				{ CompositionPass *pass = tp->createPass();
				pass->setType(CompositionPass::PT_RENDERQUAD);
				pass->setMaterialName("Ogre/Compositor/BloomBlend");
				pass->setInput(0, "rt1");
				}
			}
		}
*/

	while (mInputHandler->shutDown == false) { // shutDown == false
		
		GameSystemManager::getSingleton()->updateTimer();
		Ogre::Real time = GameSystemManager::getSingleton()->getTime();
		
		mInputHandler->capture();
		
		Ogre::WindowEventUtilities::messagePump();
		
		mWindowHandler->update();
		
		GameSystemManager::getSingleton()->getTerrain()->update();
		GameSystemManager::getSingleton()->getTerrain()->paint();
		//GameSystemManager::getSingleton()->updateSystems();
		
		//GameSystemManager::getSingleton()->getSky()->UpdateSun();
		GameSystemManager::getSingleton()->getPaging()->update();
		
		//GameSystemManager::getSingleton()->getWater()->update(time);

		GameObjectManager::getSingleton()->update();
		GameSystemManager::getSingleton()->getPhysics()->simulate(time);

		GameSystemManager::getSingleton()->getInput()->Yaw = 0;
		GameSystemManager::getSingleton()->getInput()->Pitch = 0;

		mRoot->renderOneFrame();
	}

}

unsigned int GameSceneManager::getBrush() {
	return mCurrentBrush;
}

void GameSceneManager::setBrush(unsigned int brush) {
	switch(brush)
	{
		case BRUSH_OBJECT:
			OgreQueueText::instance()->Add("Object mode enabled");
			break;
		case BRUSH_DEFORM:
			OgreQueueText::instance()->Add("Terrain deformation mode enabled");
			break;
		case BRUSH_PAINT:
			OgreQueueText::instance()->Add("Painting mode enabled");
			break;
	}
	mCurrentBrush = brush;
}
Ogre::SceneNode* GameSceneManager::getCameraNode() {
	return mCameraNode;
}
Ogre::SceneNode* GameSceneManager::getCameraPitchNode() {
	return mCameraPitchNode;
}

Ogre::SceneNode* GameSceneManager::getCameraYawNode() {
	return mCameraYawNode;
}

//Returns the name of the object that the player is currently facing to
std::string GameSceneManager::getObjectNameFromCrosshair() {
		Ogre::RaySceneQuery *rsq;
		Vector3 position;
		Vector3 direction;
		
		position = GameSceneManager::getSingleton()->getCameraNode()->getPosition();
		direction = (GameSceneManager::getSingleton()->getCameraYawNode()->getOrientation() * GameSceneManager::getSingleton()->getCameraPitchNode()->getOrientation() * Vector3::NEGATIVE_UNIT_Z);
		//direction = GameSceneManager::getSingleton()->getCamera()->getDirection(); // * Vector3::NEGATIVE_UNIT_Z);

		//GameSceneManager::getSingleton()->getSceneManager()->set
		rsq = GameSceneManager::getSingleton()->getSceneManager()->createRayQuery( Ogre::Ray(position,direction) );
		//rsq->setQueryMask(~100);
		//rsq->set
		Ogre::RaySceneQueryResult& qryResult = rsq->execute();
		
		Ogre::RaySceneQueryResult::iterator i;
		for(i = qryResult.begin(); i != qryResult.end(); i++) {
			//std::string mov_name = i->movable->getName();
			//i->movable->getParentSceneNode()->showBoundingBox(true);
			//std::string mtype = i->movable->getMovableType();

			if(!i->movable->getUserAny().isEmpty()) {
				std::string objectOwner = Ogre::any_cast<std::string> (i->movable->getUserAny()) ;
				return objectOwner;
				//gom_config.objectInHand = objectOwner;
				//gom_config.isHoldingItem = true;				
			}

			//return i->movable->getParentSceneNode();
		}
		return "";
}

Ogre::RaySceneQueryResult GameSceneManager::getOgreObjectsFromMousePos() {
	int width = GameSystemManager::getSingleton()->getInput()->getMouseStat().width;
	int height = GameSystemManager::getSingleton()->getInput()->getMouseStat().height;
	int Xabs = GameSystemManager::getSingleton()->getInput()->getMouseStat().X.abs;
	int Yabs = GameSystemManager::getSingleton()->getInput()->getMouseStat().Y.abs;
	
	Ray mouseRay = GameSceneManager::getSingleton()->getCamera()->getCameraToViewportRay(Xabs/float(width), Yabs/float(height));

	Ogre::RaySceneQuery *rsq;
	rsq = GameSceneManager::getSingleton()->getSceneManager()->createRayQuery( mouseRay );
	Ogre::RaySceneQueryResult& qryResult = rsq->execute();
	
	return qryResult;
}

//Returns the name of the object that the player is currently pointing with the mouse
std::string GameSceneManager::getObjectNameFromMousePos() {
	int width = GameSystemManager::getSingleton()->getInput()->getMouseStat().width;
	int height = GameSystemManager::getSingleton()->getInput()->getMouseStat().height;
	int Xabs = GameSystemManager::getSingleton()->getInput()->getMouseStat().X.abs;
	int Yabs = GameSystemManager::getSingleton()->getInput()->getMouseStat().Y.abs;
	
	Ray mouseRay = GameSceneManager::getSingleton()->getCamera()->getCameraToViewportRay(Xabs/float(width), Yabs/float(height));

	Ogre::RaySceneQuery *rsq;
	rsq = GameSceneManager::getSingleton()->getSceneManager()->createRayQuery( mouseRay );
	Ogre::RaySceneQueryResult& qryResult = rsq->execute();
	
	Ogre::RaySceneQueryResult::iterator i;
	for(i = qryResult.begin(); i != qryResult.end(); i++) {
		if(!i->movable->getUserAny().isEmpty()) {
			std::string objectOwner = Ogre::any_cast<std::string> (i->movable->getUserAny()) ;
			return objectOwner;
		}
	}
	return "";
}